package com.bristua.common.biz;

import com.aliyun.oss.OSSClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Date;

/**
 * Created by Administrator on 2018/9/23.
 */
@Component
@ConfigurationProperties(prefix = "file")
@PropertySource({"classpath:properties/config.properties"})
public class AliFileUploadBiz {

    private String endpoint;

    private String accessKeyId;

    private String accessKeySecret;

    private String bucketName;

    private String imgDir;

    private String vedioDir;

    private String docDir;

    private String vedioImgUrl;

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getAccessKeyId() {
        return accessKeyId;
    }

    public void setAccessKeyId(String accessKeyId) {
        this.accessKeyId = accessKeyId;
    }

    public String getAccessKeySecret() {
        return accessKeySecret;
    }

    public void setAccessKeySecret(String accessKeySecret) {
        this.accessKeySecret = accessKeySecret;
    }

    public String getBucketName() {
        return bucketName;
    }

    public void setBucketName(String bucketName) {
        this.bucketName = bucketName;
    }

    public String getImgDir() {
        return imgDir;
    }

    public void setImgDir(String imgDir) {
        this.imgDir = imgDir;
    }

    public String getVedioDir() {
        return vedioDir;
    }

    public void setVedioDir(String vedioDir) {
        this.vedioDir = vedioDir;
    }

    public String getDocDir() {
        return docDir;
    }

    public void setDocDir(String docDir) {
        this.docDir = docDir;
    }

    public String getVedioImgUrl() {
        return vedioImgUrl;
    }

    public void setVedioImgUrl(String vedioImgUrl) {
        this.vedioImgUrl = vedioImgUrl;
    }

    public String upload(InputStream inputStream, String fileName, int type){
        String objectName = imgDir;
//        String objectName = "";
//        switch (type){
//            case 0:
//                objectName = imgDir;
//                break;
//            case 1:
//                objectName = vedioDir   ;
//                break;
//            case 2:
//                objectName = docDir;
//                break;
//        }
        objectName = objectName+fileName;
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName, objectName, inputStream);
        Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
        if(type==1){
            String s = vedioImgUrl + objectName + "?x-oss-process=video/snapshot,t_7000,f_jpg,w_800,h_600,m_fast::" + ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString();
            ossClient.shutdown();
            return s;
        }
        String s = ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString();
        ossClient.shutdown();

        return s;
    }
}
